#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

#endregion

namespace TVC.Modules.Business
{
    public partial class BatchUpdate : Form
    {
        readonly string _strType = "";
        public BatchUpdate()
        {
            InitializeComponent();
        }

        public BatchUpdate(string strType, DataTable dt, MainForm form)
        {
            InitializeComponent();
            this._strType = strType;
            if (dt != null)
            {
                DataColumn dclSelect = new DataColumn("Select", Type.GetType("System.Boolean"));
                dclSelect.DefaultValue = false;
                if (!dt.Columns.Contains("Select"))
                    dt.Columns.Add(dclSelect);

                TVC.Layout.GridLayout layout = null;
                layout = new TVC.Layout.GridLayout(MainForm.ServerPath + "Resources\\Layout\\", MainForm.ServerPath + "Documents\\Layout\\" + form.PODInfo.USER_ID + "\\", _strType);
                layout.SetUpDGVWithCheck(dgvList);

                dgvList.DataSource = dt;
                dt = null;
            }
            MainForm = form;
        }
        public MainForm MainForm { get; set; }

        private void BatchUpdate_Load(object sender, EventArgs e)
        {
            txtValue.MaxLength = 10;

            if (dgvList.Rows.Count == 0)
            {
                if (!MainForm.IsContainsTables(_strType))
                    MainForm.LoadDataTable(_strType);
                dgvList.DataSource = MainForm.GetTable(_strType).Copy();
            }
            DataTable dtb = new DataTable();

            dtb.Columns.Add("ID", typeof(string));
            dtb.Columns.Add("Name", typeof(string));
            dtb.Rows.Add("OurRef", "Our Ref.");
            dtb.Rows.Add("PymtRef", "Payment Ref.");
            dtb.Rows.Add("ExcNo", "Exclaim No.");
            dtb.Rows.Add("Remark1", "Remark 1");
            dtb.Rows.Add("Remark2", "Remark 2");
            dtb.Rows.Add("Remark3", "Remark 3");
            dtb.Rows.Add("Emailed", "Emailed");
            
            
            cboField.DataSource = dtb;
            cboField.DisplayMember = "Name";
            cboField.ValueMember = "ID";
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DataTable dtb = (DataTable)dgvList.DataSource;
            dtb.AcceptChanges();
            /*for (int i = 0; i < dgvList.Rows.Count; i++)
            {
                //DataGridViewCheckBoxColumn ckb = (DataGridViewCheckBoxColumn)dgvList.Rows[i].Cells["colMark"];
                if (dgvList.Rows[i].Cells["colMark"].Value.ToString() == "true")
                {
                    BUS.ExpenseClaimControl ctr = new BUS.ExpenseClaimControl();
                    ctr.Update_FieldExpenseClaim(dgvList.Rows[i].Cells["colReference"].Value.ToString(), cboField.SelectedValue.ToString(), txtValue.Text);
                    if (cboField.SelectedValue.ToString() == "PymtRef")
                    {
                        ctr.Update_FieldExpenseClaim(dgvList.Rows[i].Cells["colReference"].Value.ToString(), "PaidDate", DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy/MM/dd"));
                    }
                }
                dtb.Rows[i]["Select"] = false;
            }*/
            for (int i = 0; i < dtb.Rows.Count; i++)
            {
                if (dtb.Rows[i]["Select"].ToString().ToLower() == "true")
                {
                    BUS.ExpenseClaimControl ctr = new BUS.ExpenseClaimControl();
                    ctr.Update_FieldExpenseClaim(dtb.Rows[i]["Reference"].ToString(), cboField.SelectedValue.ToString(), txtValue.Text);
                    if (cboField.SelectedValue.ToString() == "PymtRef")
                    {
                        string strDate = "";
                        if (txtDate.Text != "")
                            strDate = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture).ToString("yyyy/MM/dd");

                        ctr.Update_FieldExpenseClaim(dtb.Rows[i]["Reference"].ToString(), "PaidDate", strDate);

                        if (txtDate.Text != "")
                        {
                            dtb.Rows[i]["PaidDate"] = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                        }
                        else
                            dtb.Rows[i]["PaidDate"] = DBNull.Value;
                    }
                    dtb.Rows[i]["Select"] = false;
                    dtb.Rows[i][cboField.SelectedValue.ToString()] = txtValue.Text;
                }
            }
            dtb.AcceptChanges();
            dgvList.DataSource = dtb;
            dtb = null;
            MessageBox.Show("Your record is successfull updated!", "Info", MessageBoxButtons.OK, MessageBoxIcon.Information, false);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboField_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtValue.Text = "";

            if (cboField.SelectedValue.ToString() == "PymtRef")
            {
                lblDate.Visible = true;
                txtDate.Visible = true;
                txtValue.MaxLength = 100;
            }
            else
            {
                lblDate.Visible = false;
                txtDate.Visible = false;
            }
            if (cboField.SelectedValue.ToString() == "OurRef")
                txtValue.MaxLength = 10;
            else if (cboField.SelectedValue.ToString() == "ExcNo")
                txtValue.MaxLength = 50;
            else if (cboField.SelectedValue.ToString() == "Remark1")
                txtValue.MaxLength = 255;
            else if (cboField.SelectedValue.ToString() == "Remark2")
                txtValue.MaxLength = 255;
            else if (cboField.SelectedValue.ToString() == "Remark3")
                txtValue.MaxLength = 255;
            else if (cboField.SelectedValue.ToString() == "Emailed")
            {
                txtValue.MaxLength = 1;
                txtValue.Text = "0";
            }
        }

        private void txtValue_TextChanged(object sender, EventArgs e)
        {
            if (cboField.SelectedValue.ToString() == "Emailed")
                if (txtValue.Text != "0" && txtValue.Text != "1")
                {
                    MessageBox.Show("Emailed must be 0 or 1, please check!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning, false);
                    txtValue.Focus();
                    txtValue.Text = "0";
                    txtValue.SelectAll();
                }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MarkAll();
        }
        private void MarkAll()
        {
            bool mark = button1.Text == "Mark All";

            foreach (DataGridViewRow r in dgvList.Rows)
            {
                r.Cells["colSelect"].Value = mark;
            }
            if (mark) button1.Text = "Unmark All";
            else button1.Text = "Mark All";
        }
    }
}